<template>
  <div class="page-warapper">
    <PageTools>
      <template #after>
        <!-- (1,'0')传递type和pid -->
        <el-button type="primary" size="small" @click="showAddDialog(1,'0')">添加</el-button>
      </template>
    </PageTools>
    <el-card>
      <!-- row-key功能是显示树型数据--table表格里 -->
      <el-table :data="list" border row-key="id" default-expand-all>
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- v-if判断隐藏 -->
            <el-button v-if="scope.row.type===1" type="text" @click="showAddDialog(2,scope.row.id)">新增</el-button>
            <el-button type="text" @click="showEditDialog(scope.row.id)">编辑</el-button>
            <el-button type="text" @click="delPerm(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="formData.id?'编辑权限':'添加权限'" :visible="isShowDialog" @close="btnCancel">
        <el-form>
          <el-form-item label="权限名称"><el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识"><el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述"><el-input v-model="formData.description" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button size="small" type="primary" :disabled="loading" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getPermList, getPermissionDetail, updatePermission, addPermission, delPermission } from '@/api/permission'
import { listToTree } from '@/utils/index'
export default {
  data() {
    return {
      loading: false,
      list: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: ''
      }
    }
  },
  created() {
    this.loadList()
  },
  methods: {
    // 添加数据
    showAddDialog(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    // 删除数据
    async delPerm(id) {
      // 二次询问
      try {
        await this.$confirm('是否要删除')
        await delPermission(id)
        this.$message.success('删除成功')
        this.loadList()
      } catch (error) {
        console.log(error)
      }
    },
    async loadList() {
      const res = await getPermList()
      // console.log('获取的数据', res)
      // 权限数据有层级关系，所以要利用到递归函数将他转换为children的嵌套
      this.list = listToTree(res, '0')
      // console.log(this.list)
    },
    async showEditDialog(id) {
      // console.log('获取原有数据', id)
      this.isShowDialog = true
      this.formData = await getPermissionDetail(id)
      this.loadList()
    },
    // 确定
    async btnOk() {
      this.loading = true
      if (this.formData.id) {
        await updatePermission(this.formData)
      } else {
        await addPermission(this.formData)
      }

      this.$message.success('数据修改成功')
      this.isShowDialog = false
      this.loadList()
      this.loading = false
    },
    // 取消
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: ''
      }
      this.isShowDialog = false

      this.loadList()
    }
  }
}
</script>

<style>

</style>
